<?php


namespace app\portal\controller;


use app\portal\model\ContestCategoryModel;
use app\portal\model\ContestCategoryPostModel;
use app\portal\model\ContestModel;
use cmf\controller\HomeBaseController;
use think\Model;

class ContestController extends HomeBaseController
{
    public function index() {
        $user = cmf_get_current_user();
        if(!$user) {
            return redirect('/user/login');
        }
        $update_str = 0;
        # 查询当前启用的赛事
        $contestModel = new ContestModel();
        $contest = $contestModel->where('status',1)->find();
        if(!empty($contest)) {
            # 获取赛事的全部分类
            $contestCategoryModel = new ContestCategoryPostModel();
            $categoryIds = $contestCategoryModel->where('contest_id',$contest['id'])->field('category_id')->select()->toArray();
            $ids = array_column($categoryIds, 'category_id');
            $categories = ContestCategoryModel::whereIn('id',$ids)->field('name')->select()->toArray();
            # 判断赛事是否在上传作品时间内
            $end = (int)$contest['apple_end'];
            if($end < time()) {
                $update_str = 1;
            }
        }else {
            $categories = null;
        }
        $this->assign($user);
        $this->assign('categories',$categories);
        $this->assign('update_str',$update_str);
        $this->assign('contest',$contest);
        return $this->fetch('../user/profile/contest');
    }
}